package karaoke.servlet.Evento;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Evento;
import karaoke.negocio.GestionEventos;

/**
 * Servlet implementation class EventoEditarServlet
 */
@WebServlet("/EventoEditarServlet")
public class EventoEditarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventoEditarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String txtid = request.getParameter("id");
		int id = Integer.parseInt(txtid);
		
		GestionEventos negocio = new GestionEventos();
		try {
			Evento vo = negocio.obtener(id);
			request.setAttribute("vo_evento", vo);
			RequestDispatcher rd = request.getRequestDispatcher("Mantenimiento_Eventos_Editar.jsp");
			rd.forward(request, response);

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		// ACTUALIZAR LA TABLA
		String pid = request.getParameter("id");
		int ids = Integer.parseInt(pid);
		String pnombre = request.getParameter("txtNombre");//varchar
		String phora = request.getParameter("timefecha");//varchar
		String pcapacidad = request.getParameter("txtCapacidad");//int
		int capacidad = Integer.parseInt(pcapacidad);
		String pdescrip = request.getParameter("txtDescripcion");//varchar
		String plocal = request.getParameter("txtlocal");//int
		int local = Integer.parseInt(plocal);
		
		GestionEventos evento = new GestionEventos();
		try {
			SimpleDateFormat fecha = new SimpleDateFormat("yyyy-MM-dd"); 
			java.util.Date date=fecha.parse(request.getParameter("datefecha"));
			evento.actualizar(ids, pnombre, date, phora, capacidad, pdescrip, local);
			response.sendRedirect(request.getContextPath() + "/Mantenimiento_Eventos.jsp");
		} catch (DAOExcepcion | ParseException e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}
